<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class SendMsgConfig extends \app\BaseModel
{
    protected $name = "massage_send_msg_config";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $this->dataAdd($dis);
            $data = $this->where($dis)->find();
        }
        return !empty($data) ? $data->toArray() : [];
    }
    public function initData($uniacid)
    {
        $data = $this->dataInfo(["uniacid" => $uniacid]);
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if(!empty($config["gzh_appid"])) {
            $update = ["help_tmpl_id" => $config["help_tmpl_id"], "order_tmp_id" => $config["order_tmp_id"], "cancel_tmp_id" => $config["cancel_tmp_id"], "coachupdate_tmp_id" => $config["coachupdate_tmp_id"], "gzh_appid" => $config["gzh_appid"]];
            $this->dataUpdate(["id" => $data["id"]], $update);
            $prefix = longbing_get_prefix();
            $sql = "            \nALTER TABLE `" . $prefix . "shequshop_school_config` DROP COLUMN  `help_tmpl_id`;\nALTER TABLE `" . $prefix . "shequshop_school_config` DROP COLUMN  `order_tmp_id`;\nALTER TABLE `" . $prefix . "shequshop_school_config` DROP COLUMN  `cancel_tmp_id`;\nALTER TABLE `" . $prefix . "shequshop_school_config` DROP COLUMN  `coachupdate_tmp_id`;\nALTER TABLE `" . $prefix . "shequshop_school_config` DROP COLUMN  `gzh_appid`;\n\n                \n";
            $sql = str_replace(PHP_EOL, "", $sql);
            $sqlArray = explode(";", $sql);
            foreach ($sqlArray as $_value) {
                if(!empty($_value)) {
                    try {
                        \think\facade\Db::query($_value);
                    } catch (\Exception $e) {
                        if(!APP_DEBUG) {
                        }
                    }
                }
            }
        }
        return true;
    }
    public function webRefundMsgAdmin($order)
    {
        $order_wechat_agent_status = getConfigSetting($order["uniacid"], "order_wechat_agent_status");
        if(!empty($order["admin_id"]) && $order_wechat_agent_status == 1) {
            $uniacid = $order["uniacid"];
            $config_model = new SendMsgConfig();
            $config_model->initData($uniacid);
            $x_config = $config_model->dataInfo(["uniacid" => $uniacid]);
            if(empty($x_config["gzh_appid"]) || empty($x_config["cancel_tmp_id"])) {
                return false;
            }
            $admin_model = new Admin();
            $user_model = new User();
            $user_id = $admin_model->where(["id" => $order["admin_id"]])->value("user_id");
            $openid = $user_model->where(["id" => $user_id])->value("web_openid");
            $wx_setting = new \longbingcore\wxcore\WxSetting($uniacid);
            $access_token = $wx_setting->getGzhToken();
            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
            $order_model = new Order();
            $start_time = $order_model->where(["id" => $order["order_id"]])->value("start_time");
            $key = explode("&", $x_config["cancel_tmp_id"]);
            for ($i = 1; $i < 4; $i++) {
                $arr[$i] = !empty($key[$i]) ? $key[$i] : "keyword" . $i;
            }
            $data = ["touser" => $openid, "appid" => $x_config["gzh_appid"], "url" => "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/agent/pages/refund/detail?agent=1&id=" . $order["id"], "template_id" => $key[0], "data" => ["first" => ["value" => "您的技师有一笔订单正在申请退款", "color" => "#93c47d"], $arr[1] => ["value" => implode(",", array_column($order["order_goods"], "goods_name")), "color" => "#93c47d"], $arr[2] => ["value" => date("Y-m-d H:i", $start_time), "color" => "#0000ff"], $arr[3] => ["value" => $order["text"], "color" => "#0000ff"]]];
            $data = json_encode($data);
            $tmp = ["url" => $url, "data" => $data];
            $rest = lbCurlPost($tmp["url"], $tmp["data"]);
            $rest = json_decode($rest, true);
            return $rest;
        }
        return true;
    }
    public function wechatRefundMsgAdmin($order)
    {
        if(empty($order["admin_id"])) {
            return false;
        }
        $uniacid = $order["uniacid"];
        $config_model = new SendMsgConfig();
        $config_model->initData($uniacid);
        $x_config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if(empty($x_config["gzh_appid"]) || empty($x_config["order_tmp_id"])) {
            return false;
        }
        $config = longbingGetAppConfig($uniacid);
        $admin_model = new Admin();
        $user_model = new User();
        $user_id = $admin_model->where(["id" => $order["admin_id"]])->value("user_id");
        $openid = $user_model->where(["id" => $user_id])->value("wechat_openid");
        $access_token = longbingGetAccessToken($uniacid);
        $page = "user/pages/order/detail?id=" . $order["id"];
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" . $access_token;
        $order_model = new Order();
        $start_time = $order_model->where(["id" => $order["order_id"]])->value("start_time");
        $data = ["touser" => $openid, "mp_template_msg" => ["appid" => $x_config["gzh_appid"], "url" => "http://weixin.qq.com/download", "template_id" => $x_config["cancel_tmp_id"], "miniprogram" => ["appid" => $config["appid"], "page" => $page], "data" => ["first" => ["value" => "您的技师有一笔订单正在申请退款", "color" => "#93c47d"], "keyword1" => ["value" => implode(",", array_column($order["order_goods"], "goods_name")), "color" => "#93c47d"], "keyword2" => ["value" => date("Y-m-d H:i", $start_time), "color" => "#0000ff"], "keyword3" => ["value" => $order["text"], "color" => "#0000ff"]]]];
        $data = json_encode($data);
        $tmp = ["url" => $url, "data" => $data];
        $rest = lbCurlPost($tmp["url"], $tmp["data"]);
        $rest = json_decode($rest, true);
        return $rest;
    }
    public function wechatRefundMsgCompany($order)
    {
        $uniacid = $order["uniacid"];
        $config_model = new SendMsgConfig();
        $user_model = new User();
        $config_model->initData($uniacid);
        $x_config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if(empty($x_config["gzh_appid"]) || empty($x_config["order_tmp_id"])) {
            return false;
        }
        $admin_id = getConfigSetting($order["uniacid"], "wechat_tmpl_admin");
        if(empty($admin_id)) {
            return false;
        }
        $admin_id = explode(",", $admin_id);
        foreach ($admin_id as $value) {
            $openid = $user_model->where(["id" => $value])->value("openid");
            $config = longbingGetAppConfig($uniacid);
            $access_token = longbingGetAccessToken($uniacid);
            $page = "user/pages/order/detail?id=" . $order["id"];
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" . $access_token;
            $order_model = new Order();
            $start_time = $order_model->where(["id" => $order["order_id"]])->value("start_time");
            $data = ["touser" => $openid, "mp_template_msg" => ["appid" => $x_config["gzh_appid"], "url" => "http://weixin.qq.com/download", "template_id" => $x_config["cancel_tmp_id"], "miniprogram" => ["appid" => $config["appid"], "page" => $page], "data" => ["first" => ["value" => "您的技师有一笔订单正在申请退款", "color" => "#93c47d"], "keyword1" => ["value" => implode(",", array_column($order["order_goods"], "goods_name")), "color" => "#93c47d"], "keyword2" => ["value" => date("Y-m-d H:i", $start_time), "color" => "#0000ff"], "keyword3" => ["value" => $order["text"], "color" => "#0000ff"]]]];
            $data = json_encode($data);
            $tmp = ["url" => $url, "data" => $data];
            $rest = lbCurlPost($tmp["url"], $tmp["data"]);
            $rest = json_decode($rest, true);
            return $rest;
        }
        return true;
    }
    public function webRefundMsgCompany($order)
    {
        $order_wechat_admin_status = getConfigSetting($order["uniacid"], "order_wechat_admin_status");
        if(!empty($order["admin_id"]) && $order_wechat_admin_status == 0) {
            return true;
        }
        $user_model = new User();
        $uniacid = $order["uniacid"];
        $config_model = new SendMsgConfig();
        $config_model->initData($uniacid);
        $x_config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if(empty($x_config["gzh_appid"]) || empty($x_config["cancel_tmp_id"])) {
            return false;
        }
        $admin_id = getConfigSetting($order["uniacid"], "wechat_tmpl_admin");
        if(empty($admin_id)) {
            return false;
        }
        $admin_id = explode(",", $admin_id);
        foreach ($admin_id as $value) {
            $openid = $user_model->where(["id" => $value])->value("openid");
            $wx_setting = new \longbingcore\wxcore\WxSetting($uniacid);
            $access_token = $wx_setting->getGzhToken();
            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
            $order_model = new Order();
            $start_time = $order_model->where(["id" => $order["order_id"]])->value("start_time");
            $key = explode("&", $x_config["cancel_tmp_id"]);
            for ($i = 1; $i < 4; $i++) {
                $arr[$i] = !empty($key[$i]) ? $key[$i] : "keyword" . $i;
            }
            $data = ["touser" => $openid, "appid" => $x_config["gzh_appid"], "url" => "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/agent/pages/refund/detail?agent=0&id=" . $order["id"], "template_id" => $key[0], "data" => ["first" => ["value" => "您的技师有一笔订单正在申请退款", "color" => "#93c47d"], $arr[1] => ["value" => implode(",", array_column($order["order_goods"], "goods_name")), "color" => "#93c47d"], $arr[2] => ["value" => date("Y-m-d H:i", $start_time), "color" => "#0000ff"], $arr[3] => ["value" => $order["text"], "color" => "#0000ff"]]];
            $data = json_encode($data);
            $tmp = ["url" => $url, "data" => $data];
            $rest = lbCurlPost($tmp["url"], $tmp["data"]);
            $rest = json_decode($rest, true);
        }
        return isset($rest) ? $rest : true;
    }
    public function getNoticeType($type)
    {
        switch ($type) {
            case 1:
                $data["text"] = "未接单通知";
                break;
            case 2:
                $data["text"] = "拒单通知";
                break;
            case 3:
                $data["text"] = "迟到通知";
                break;
            case 4:
                $data["text"] = "跳单通知";
                break;
            default:

        }
        return $data;
    }
    public function webOrderServiceNoticeCompany($order, $type)
    {
        $order_wechat_admin_status = getConfigSetting($order["uniacid"], "order_wechat_admin_status");
        if(!empty($order["admin_id"]) && $order_wechat_admin_status == 0) {
            return true;
        }
        $text = $this->getNoticeType($type);
        $user_model = new User();
        $uniacid = $order["uniacid"];
        $config_model = new SendMsgConfig();
        $config_model->initData($uniacid);
        $x_config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if(empty($x_config["gzh_appid"]) || empty($x_config["order_service_tmpl_id"])) {
            return true;
        }
        $admin_id = getConfigSetting($order["uniacid"], "wechat_tmpl_admin");
        if(empty($admin_id)) {
            return true;
        }
        $admin_id = explode(",", $admin_id);
        foreach ($admin_id as $value) {
            $openid = $user_model->where(["id" => $value])->value("openid");
            $wx_setting = new \longbingcore\wxcore\WxSetting($uniacid);
            $access_token = $wx_setting->getGzhToken();
            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
            $key = explode("&", $x_config["order_service_tmpl_id"]);
            for ($i = 1; $i < 3; $i++) {
                $arr[$i] = !empty($key[$i]) ? $key[$i] : "keyword" . $i;
            }
            $data = ["touser" => $openid, "appid" => $x_config["gzh_appid"], "url" => "https://" . $_SERVER["HTTP_HOST"] . "/h5/", "template_id" => $key[0], "data" => [$arr[1] => ["value" => $order["order_code"], "color" => "#93c47d"], $arr[2] => ["value" => $text["text"], "color" => "#0000ff"]]];
            $data = json_encode($data);
            $tmp = ["url" => $url, "data" => $data];
            $rest = lbCurlPost($tmp["url"], $tmp["data"]);
            $rest = json_decode($rest, true);
        }
        return $rest;
    }
    public function webOrderServiceNoticeAdmin($order, $type)
    {
        $order_wechat_agent_status = getConfigSetting($order["uniacid"], "order_wechat_agent_status");
        if(!empty($order["admin_id"]) && $order_wechat_agent_status == 1) {
            $text = $this->getNoticeType($type);
            $uniacid = $order["uniacid"];
            $config_model = new SendMsgConfig();
            $config_model->initData($uniacid);
            $x_config = $config_model->dataInfo(["uniacid" => $uniacid]);
            if(empty($x_config["gzh_appid"]) || empty($x_config["order_service_tmpl_id"])) {
                return true;
            }
            $admin_model = new Admin();
            $user_model = new User();
            $user_id = $admin_model->where(["id" => $order["admin_id"]])->value("user_id");
            $openid = $user_model->where(["id" => $user_id])->value("web_openid");
            $wx_setting = new \longbingcore\wxcore\WxSetting($uniacid);
            $access_token = $wx_setting->getGzhToken();
            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
            $key = explode("&", $x_config["order_service_tmpl_id"]);
            for ($i = 1; $i < 3; $i++) {
                $arr[$i] = !empty($key[$i]) ? $key[$i] : "keyword" . $i;
            }
            $data = ["touser" => $openid, "appid" => $x_config["gzh_appid"], "url" => "https://" . $_SERVER["HTTP_HOST"] . "/h5", "template_id" => $key[0], "data" => [$arr[1] => ["value" => $order["order_code"], "color" => "#93c47d"], $arr[2] => ["value" => $text["text"], "color" => "#0000ff"]]];
            $data = json_encode($data);
            $tmp = ["url" => $url, "data" => $data];
            $rest = lbCurlPost($tmp["url"], $tmp["data"]);
            $rest = json_decode($rest, true);
        }
        return true;
    }
    public function orderServiceNotice($uniacid)
    {
        $key = "orderServiceNotice_key_value" . date("Y-m-d H:i");
        incCache($key, 1, $uniacid);
        $value = getCache($key, $uniacid);
        if($value == 1) {
            $this->latOrderNotice($uniacid);
            $this->unacceptedOrdersNotice($uniacid);
            $this->jumpOrdersNotice($uniacid);
            $this->unpaidOrdersNotice($uniacid);
        }
        decCache($key, 1, $uniacid);
        return true;
    }
    public function latOrderNotice($uniacid)
    {
        $setting = getConfigSettingArr($uniacid, ["wechat_tmpl", "service_lat_type", "service_lat_minute"]);
        $order_data_model = new OrderData();
        $dis[] = ["a.uniacid", "=", $uniacid];
        $dis[] = ["a.is_add", "=", 0];
        $dis[] = ["b.late_notice", "=", 0];
        $dis[] = ["a.pay_type", "in", [3, 4]];
        $notice_model = new NoticeList();
        $dis[] = ["a.store_id", "=", 0];
        if($setting["service_lat_type"] == 0) {
            $data = \think\facade\Db::name("massage_service_order_list")->alias("a")->join("massage_service_order_list_data b", "a.id = b.order_id")->where("start_time", "<", time() + $setting["service_lat_minute"] * 60)->where($dis)->field("a.*")->order("id desc")->limit(10)->select()->toArray();
        } else {
            $data = \think\facade\Db::name("massage_service_order_list")->alias("a")->join("massage_service_order_list_data b", "a.id = b.order_id")->where("start_time", "<", time() - $setting["service_lat_minute"] * 60)->where($dis)->field("a.*")->order("id desc")->limit(10)->select()->toArray();
        }
        if(!empty($data)) {
            foreach ($data as $v) {
                $notice_model->dataAdd($uniacid, $v["id"], 5, $v["admin_id"]);
                $order_data_model->dataUpdate(["order_id" => $v["id"]], ["late_notice" => 1]);
                $this->webOrderServiceNoticeAdmin($v, 3);
                if($setting["wechat_tmpl"] == 1) {
                    $this->webOrderServiceNoticeCompany($v, 3);
                }
            }
        }
        return true;
    }
    public function unacceptedOrdersNotice($uniacid)
    {
        $setting = getConfigSettingArr($uniacid, ["wechat_tmpl", "coach_receiving_minute"]);
        $order_model = new Order();
        $order_data_model = new OrderData();
        $dis[] = ["a.uniacid", "=", $uniacid];
        $dis[] = ["a.pay_type", "=", 2];
        $dis[] = ["b.receiving_order_notice", "=", 0];
        $notice_model = new NoticeList();
        $data = \think\facade\Db::name("massage_service_order_list")->alias("a")->join("massage_service_order_list_data b", "a.id = b.order_id")->where("a.pay_time", "<", time() - $setting["coach_receiving_minute"] * 60)->where($dis)->field("a.*")->order("id desc")->limit(10)->select()->toArray();
        if(!empty($data)) {
            foreach ($data as $v) {
                $find = $notice_model->dataInfo(["type" => 4, "order_id" => $v["id"]]);
                $notice_model->dataAdd($uniacid, $v["id"], 4, $v["admin_id"]);
                $order_data_model->dataUpdate(["order_id" => $v["id"]], ["receiving_order_notice" => 1]);
                if(empty($find)) {
                    $this->webOrderServiceNoticeAdmin($v, 1);
                    if($setting["wechat_tmpl"] == 1) {
                        $this->webOrderServiceNoticeCompany($v, 1);
                    }
                }
            }
        }
        return true;
    }
    public function jumpOrdersNotice($uniacid)
    {
        $setting = getConfigSettingArr($uniacid, ["wechat_tmpl", "jump_order_minute", "jump_order_distance"]);
        $order_model = new Order();
        $coach_model = new Coach();
        $notice_model = new NoticeList();
        $dis[] = ["uniacid", "=", $uniacid];
        $dis[] = ["pay_type", "=", 7];
        $dis[] = ["is_safe", "=", 0];
        $dis[] = ["store_id", "=", 0];
        $dis[] = ["is_add", "=", 0];
        $dis[] = ["coach_id", ">", 0];
        $start_time = time() - $setting["jump_order_minute"] * 60 - 3600;
        $end_time = time() - $setting["jump_order_minute"] * 60;
        $dis[] = ["order_end_time", "between", $start_time . "," . $end_time];
        $data = $order_model->where($dis)->order("id desc")->limit(10)->select()->toArray();
        if(!empty($data)) {
            foreach ($data as $v) {
                $lat = !empty($v["address_info"]) ? $v["address_info"]["lat"] : 0;
                $lng = !empty($v["address_info"]) ? $v["address_info"]["lng"] : 0;
                $coach_info = $coach_model->dataInfo(["id" => $v["coach_id"]]);
                $coach_lat = !empty($coach_info) ? $coach_info["lat"] : 90;
                $coach_lng = !empty($coach_info) ? $coach_info["lng"] : 90;
                $distance = getDriveDistance($coach_lng, $coach_lat, $lng, $lat, $uniacid);
                if(0 < $setting["jump_order_distance"] && $distance < $setting["jump_order_distance"] * 1000) {
                    $notice_model->dataAdd($uniacid, $v["id"], 6, $v["admin_id"]);
                    $this->webOrderServiceNoticeAdmin($v, 4);
                    if($setting["wechat_tmpl"] == 1) {
                        $this->webOrderServiceNoticeCompany($v, 4);
                    }
                    $order_model->dataUpdate(["id" => $v["id"]], ["is_safe" => 2]);
                } else {
                    $order_model->dataUpdate(["id" => $v["id"]], ["is_safe" => 1]);
                }
            }
        }
        return true;
    }
    public function sendOrderTmplAdmin($order)
    {
        $config_model = new HelpConfig();
        $user_model = new User();
        $admin_model = new Admin();
        $x_config = $this->dataInfo(["uniacid" => $order["uniacid"]]);
        if(empty($x_config["gzh_appid"]) || empty($x_config["order_tmp_id"])) {
            return false;
        }
        $virtual_config_model = new \app\virtual\model\Config();
        $mobile_auth = $virtual_config_model->getVirtualAuth($order["uniacid"]);
        $config = $config_model->dataInfo(["uniacid" => $order["uniacid"]]);
        $wx_setting = new \longbingcore\wxcore\WxSetting($order["uniacid"]);
        $access_token = $wx_setting->getGzhToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
        $key = explode("&", $x_config["order_tmp_id"]);
        for ($i = 1; $i < 6; $i++) {
            $arr[$i] = !empty($key[$i]) ? $key[$i] : "keyword" . $i;
        }
        $coach_model = new Coach();
        $coach_name = $coach_model->where(["id" => $order["coach_id"]])->value("coach_name");
        $order["address_info"]["address_info"] = !empty($order["address_info"]["address_info"]) ? $order["address_info"]["address_info"] : $order["address_info"]["address"];
        if($config["order_tmpl_admin_status"] == 1 && is_array($config["order_tmpl_text"]) && (empty($order["admin_id"]) || $config["order_tmpl_notice_admin"])) {
            foreach ($config["order_tmpl_text"] as $value) {
                $openid = $user_model->where(["id" => $value])->value("web_openid");
                $data = ["touser" => $openid, "appid" => $x_config["gzh_appid"], "url" => "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/agent/pages/order/detail?agent=0&id=" . $order["id"], "template_id" => $key[0], "data" => [$arr[1] => ["value" => mb_substr(implode(",", array_column($order["order_goods"], "goods_name")), 0, 20), "color" => "#93c47d"], $arr[2] => ["value" => mb_substr($order["address_info"]["user_name"] . "(技师:" . $coach_name . ")", 0, 20), "color" => "#0000ff"], $arr[3] => ["value" => !$mobile_auth ? $order["address_info"]["mobile"] : "-", "color" => "#0000ff"], $arr[4] => ["value" => $order["order_code"], "color" => "#0000ff"], $arr[5] => ["value" => mb_substr($order["address_info"]["address_info"], 0, 20), "color" => "#0000ff"]]];
                $data = json_encode($data);
                $tmp = ["url" => $url, "data" => $data];
                $rest = lbCurlPost($tmp["url"], $tmp["data"]);
            }
        }
        if($config["order_tmpl_agent_status"] == 1 && !empty($order["admin_id"])) {
            $user_id = $admin_model->where(["id" => $order["admin_id"]])->value("user_id");
            $openid = $user_model->where(["id" => $user_id])->value("web_openid");
            $data = ["touser" => $openid, "appid" => $x_config["gzh_appid"], "url" => "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/agent/pages/order/detail?agent=1&id=" . $order["id"], "template_id" => $key[0], "data" => [$arr[1] => ["value" => mb_substr(implode(",", array_column($order["order_goods"], "goods_name")), 0, 20), "color" => "#93c47d"], $arr[2] => ["value" => mb_substr($order["address_info"]["user_name"] . "(技师:" . $coach_name . ")", 0, 20), "color" => "#0000ff"], $arr[3] => ["value" => !$mobile_auth ? $order["address_info"]["mobile"] : "-", "color" => "#0000ff"], $arr[4] => ["value" => $order["order_code"], "color" => "#0000ff"], $arr[5] => ["value" => mb_substr($order["address_info"]["address_info"], 0, 20), "color" => "#0000ff"]]];
            $data = json_encode($data);
            $tmp = ["url" => $url, "data" => $data];
            $rest = lbCurlPost($tmp["url"], $tmp["data"]);
        }
        return true;
    }
    public function sendCouponNotice($user, $coupon)
    {
        $x_config = $this->dataInfo(["uniacid" => $coupon["uniacid"]]);
        if(empty($x_config["gzh_appid"]) || empty($x_config["coupon_notice_tmpl_id"])) {
            return false;
        }
        $end_time = $coupon["time_limit"] == 1 ? time() + $coupon["day"] * 86400 : $coupon["end_time"];
        $user_model = new User();
        $dis[] = ["id", "in", $user];
        $user = $user_model->userSelect($dis, "web_openid");
        $wx_setting = new \longbingcore\wxcore\WxSetting($coupon["uniacid"]);
        $access_token = $wx_setting->getGzhToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
        $key = explode("&", $x_config["coupon_notice_tmpl_id"]);
        for ($i = 1; $i < 4; $i++) {
            $arr[$i] = !empty($key[$i]) ? $key[$i] : "keyword" . $i;
        }
        if(!empty($user)) {
            foreach ($user as $value) {
                $data = ["touser" => $value["web_openid"], "appid" => $x_config["gzh_appid"], "url" => "https://" . $_SERVER["HTTP_HOST"] . "/h5", "template_id" => $key[0], "data" => [$arr[1] => ["value" => date("Y-m-d H:i", $end_time), "color" => "#93c47d"], $arr[2] => ["value" => round($coupon["discount"], 2) . "元", "color" => "#0000ff"], $arr[3] => ["value" => mb_substr($coupon["title"], 0, 20), "color" => "#0000ff"]]];
                $data = json_encode($data);
                $tmp = ["url" => $url, "data" => $data];
                $rest = lbCurlPost($tmp["url"], $tmp["data"]);
            }
            return $rest;
        } else {
            return true;
        }
    }
    public function unpaidOrdersNotice($uniacid)
    {
        $timeout = getConfigSetting($uniacid, "order_pay_timeout_remind");
        if(empty($timeout)) {
            return false;
        }
        $order_id = \app\baiying\model\BaiYingPhoneRecord::where(["uniacid" => $uniacid, "type" => 1])->column("order_id");
        $timeout = time() - $timeout * 60;
        $where = [["create_time", "<", $timeout], ["pay_type", "=", 1], ["over_time", ">", time()], ["id", "not in", $order_id]];
        $order = Order::where($where)->select()->toArray();
        if(empty($order)) {
            return false;
        }
        $address = OrderAddress::whereIn("order_id", array_column($order, "id"))->field("uniacid,order_id,mobile,user_name")->select()->toArray();
        $binying = new \longbingcore\wxcore\BaiYing($uniacid);
        $res = $binying->water($address);
        return $res;
    }
}

?>